package com.kibey.tools;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by mchwind on 18/9/14.
 * 找出没有翻译的资源
 */
public class FindUnTranslateStrings {

    public static void main(String[] args) throws IOException {
        find("app");
    }

    public static void find(String moduleName) throws IOException {
        File zhFile = MergeStringsFromApp.getFiles(new String[]{"values/", "strings.xml", moduleName});
        File esFile = Utils.getResFile("strings", "strings_pt.xml");
        compare(zhFile, esFile, "pt");
    }

    private static void compare(File zhFile, File esFile, String language) throws IOException {
        List<Utils.StringsEntry> zhMap = Utils.strings2Entry(zhFile);
        Map<String, String> esMap = Utils.getStrings4Map(esFile);
        File noTranslate = Utils.getResFile("no_translate", language + ".txt");
        File noTranslateXml = Utils.getResFile("no_translate", language + ".xml");

        noTranslate.delete();
        noTranslate.createNewFile();
        noTranslateXml.delete();
        noTranslateXml.createNewFile();

        for (Map.Entry<String, String> entry : zhMap) {
            if (!esMap.containsKey(entry.getKey())) {
                FileUtils.write(noTranslate,  entry.getValue() + "\n", true);
                FileUtils.write(noTranslateXml, Utils.getAndroidStringItem(entry), true);
            }

        }
    }

    public static void print2keyValue(File file) throws IOException {

        List<String> list = FileUtils.readLines(file, "UTF-8");

        for (String s : list) {
            String key = Utils.getStringsName(s);
            String value = Utils.getStringsValue(s);

            if (null != key) {
                System.out.println(key + "\t" + value);
            }
        }
    }
}
